package cn.com.homeyoda.aimate.aihealth.ui.protocol;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.lifecycle.ViewModelProviders;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.stx.xhb.xbanner.transformers.Transformer;
import com.wzq.mvvmsmart.utils.ToastUtils;

import java.util.ArrayList;

import cn.com.homeyoda.aimate.aihealth.BR;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.bean.KePuBannerBean;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityProtocolBinding;

public class ProtocolMainActivity extends BaseActivity<ActivityProtocolBinding, ProtocolViewModel> implements View.OnClickListener {

    private final ArrayList<KePuBannerBean> bannerList = new ArrayList<>();

    public static void start(Context context) {
        context.startActivity(new Intent(context, ProtocolMainActivity.class));
    }

    @Override
    public ProtocolViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(ProtocolViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_protocol;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.getBanner();
        initView();
    }

    private void initView() {
        binding.titleBar.ivBack.setOnClickListener(this);
        binding.btnNext.setOnClickListener(this);
        binding.btnNext.setText("下一页(1/2)");
        // binding.btnPre.setOnClickListener(this);
        // binding.btnNext.setOnClickListener(this);
        binding.titleBar.tvTitle.setText(getString(R.string.home_device));
    }

    private void setBannerView() {
        // XBanner banner = binding.protocolBanner;
        binding.protocolBanner.setBannerData(bannerList);
        binding.protocolBanner.loadImage((banner12, model, view, position) -> {
            // KePuBannerBean bannerBean = (KePuBannerBean) model;
            Glide.with(this)
                    // .load(bannerBean.getXBannerUrl())
                    .load(R.mipmap.test)
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(16)))
                    .into((ImageView) view);
        });
        binding.protocolBanner.setPageTransformer(Transformer.Default);
        binding.protocolBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (bannerList.size() == position + 1) {
                    binding.btnNext.setText("我已知晓");
                } else {
                    binding.btnNext.setText("下一页(" + (position + 1) + "/" + bannerList.size() + ")");
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.bannerData.observe(this, bannerList -> {
            this.bannerList.clear();
            this.bannerList.addAll(bannerList);
            setBannerView();
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.btn_next) {
            ViewPager vp = binding.protocolBanner.getViewPager();
            int next = vp.getCurrentItem() + 1;
            if (next == bannerList.size()) {
                if (!binding.cbProtocol.isChecked()) {
                    ToastUtils.showShort("请勾选同意协议");
                    return;
                }
                finish();
            } else {
                vp.setCurrentItem(next, true);
            }
        }
    }
}